/**业务管理流程平台版本1.0,生成时间:2017-04-13 13:16:48**/
package com.xbdun.dao.bmp.mapper;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;

import com.xbdun.dao.bmp.dto.MenuNodeDto;
import com.xbdun.dao.bmp.dto.MenuPlatDto;
import com.xbdun.dao.bmp.dto.PlatformInfoDto;
import com.xbdun.dao.bmp.dto.PlatformMenuDto;
//192.168.0.11:8088/gitbucket/git/bmp/xbdun-dao-bmp.git
import com.xbdun.dao.bmp.entity.XbdunSysMenuDo;
import com.xbdun.dao.bmp.provider.SysMenuSqlProvider;


/**
 * 菜单表
 * @author 张国涛
 * @2017-04-13 13:16:48
 */
@Mapper
public interface SysMenuMapper{

	/**
	 * 根据主键获取对象
	 * @return
	 */
	@Select("SELECT * FROM xbdun_sys_menu where id=#{id}")
	public XbdunSysMenuDo getXbdunSysMenuById(@Param("id")long id);
	@Select("SELECT * FROM xbdun_sys_menu where code=${code}")
	public XbdunSysMenuDo getXbdunSysMenuByCode(@Param("code")Long code);

	/**
	 * 新增
	 * @param XbdunSysMenuDo 菜单表
	 */
	@InsertProvider(type=SysMenuSqlProvider.class,method="addXbdunSysMenuSql")
	@SelectKey(statement="SELECT LAST_INSERT_ID() AS ID", before = false, keyProperty = "a.id", resultType = Long.class)
	public Long addXbdunSysMenu(@Param("a")XbdunSysMenuDo xbdunSysMenuDo,@Param("platCode")Integer platCode);
	@Update("UPDATE xbdun_sys_menu SET sort=#{code},url=REPLACE(url,'#CODE#',code) WHERE id=#{id}")
	public int updateMenuSort(@Param("id")long id,@Param("code")long code);
	@Select("SELECT RPAD(code,12,'0') FROM xbdun_sys_menu WHERE id=#{0}")
	public long selectToCode(long id);
	
	/**
	 * 删除数据根据主键
	 * @param id
	 */
	@Delete("DELETE FROM xbdun_sys_menu WHERE id = #{id}")
	public void delXbdunSysMenuById(@Param("id")long id);
	
	/**
	 * 判断是否存在,不为null则是存在
	 * @param param
	 * @return Long
	 */
	@SelectProvider(type=SysMenuSqlProvider.class,method="existSql")
	public int existXbdunSysMenu(@Param("param")Map<String,Object> param);
	/**
	 * 动态更新参数,参数中Map可以必须跟Key为Where的更新条件<br/>
	 * <pre>
	 * 	例如：
	 *  Map<String,Object> setting = new HashMap<String,Object>();
	 *  setting.put("email", "360730440@qq.com");
	 *
	 *  Map<String,Object> where = new HashMap<String,Object>();
	 *  where.put("userId", "1222");
	 * </pre>
	 * Map的key值就是数据库字段<br/>
	 * Map的value就是值
	 */
	@UpdateProvider(type=SysMenuSqlProvider.class,method="updateDynXbdunSysMenuSql")
	public int updateDynXbdunSysMenu(@Param("param")Map<String,Object> param,@Param("_where")Map<String,Object> where);
	
	//------------------------//start分页查询--------------------------------------
	/**
	 * 分页查询 
	 * <pre>
	 * 	例如：
	 *  Map<String,Object> where = new HashMap<String,Object>();
	 *  param.put("orderby", "param.name,param age desc");
	 * </pre>
	 * @param _where where条件，现在支持And条件类型
	 * @param orderBy 排序条件
	 * @param pageNum 当前索引页
	 * @param numPerPage 每页显示条数
	 * @return
	 */
	@SelectProvider(type=SysMenuSqlProvider.class,method="queryPagerSql")
	public List<XbdunSysMenuDo> queryPager(@Param("_where")Map<String,Object> where,@Param("_orderby")String orderBy,@Param("pageNum")int pageNum,@Param("numPerPage")int numPerPage);
	
	/**
	 * 分页查询返回总数量
	 * <pre>
	 * 	例如：
	 *  Map<String,Object> where = new HashMap<String,Object>();
	 *  param.put("orderby", "param.name,param age desc");
	 * </pre>
	 * @param where 参数为where约束参数
	 * @return
	 */
	@SelectProvider(type=SysMenuSqlProvider.class,method="queryPagerCountSql")
	public Long queryPagerCount(@Param("_where")Map<String,Object> where);
	//-------------------------//end--------------------------------------------

	/**
	 * 查询指定父级菜单的子菜单最大菜单code后一位
	 * @param param
	 * @return Long
	 */
	@SelectProvider(type=SysMenuSqlProvider.class,method="getAddMenuCodeSql")
	public Long getAddMenuCode(@Param("code")long code);

	/**
	 * 查询机构的版本号
	 * @param id
	 * @return String
	 */
	@Select("SELECT version FROM xbdun_sys_menu where id=#{id}")
	public Timestamp getMenuVersion(@Param("id")long id);
	
	/**
	 * @Title: queryAllMenu
	 * @Description: 获取当前机构授权菜单树状结构
	 * @param baseMenuCode			基础设置菜单
	 * @return List<MenuNodeDto>    返回类型
	 */
	@Select("SELECT m.code id, LEFT(m.code,LENGTH(m.code)-3) pId,m.name,m.hit title,IF(IFNULL(om.code,0)=0,0,1) checked FROM xbdun_sys_menu AS m LEFT JOIN (SELECT b.code FROM xbdun_sys_org_menus b WHERE b.orgCode = ${orgCode}) om ON m.code = om.code WHERE m.blDel=2 ORDER BY m.sort ASC")
	public List<MenuNodeDto> queryAllMenu(@Param("orgCode")long orgCode);

	/**
	* @Title: queryTreeMenu
	* @Description: 获取机构菜单树状结构
	* @param orgCode			当前机构
	* @return List<MenuNodeDto>    返回类型
	*/
	@SelectProvider(type=SysMenuSqlProvider.class,method="queryOrgMenuSql")
	public List<MenuNodeDto> queryOrgMenu(@Param("orgCode")long orgCode,@Param("agentCode")long agentCode,@Param("menuIds")Long[] menuIds);
	
	/**
	* @Title: queryAgentOrgTreeMenu
	* @Description: TODO(这里用一句话描述这个方法的作用)
	* @param orgCode
	* @param menuIds
	* @return    设定文件
	* @return List<MenuNodeDto>    返回类型
	 */
	@SelectProvider(type=SysMenuSqlProvider.class,method="queryAgentOrgMenuSql")
	public List<MenuNodeDto> queryAgentOrgMenu(@Param("orgCode")long orgCode,@Param("menuIds")Long[] menuIds);
	
	/**
	* @Title: querySelfOrgMenu
	* @Description: 查询本机构的所有菜单
	* @param orgCode	当前机构ID
	* @return List<MenuNodeDto>    返回类型
	 */
	@Select("SELECT a.name,a.hit title,a.code id,IF(LENGTH(a.code)=3,0,LEFT (a.code, LENGTH(a.code) - 3)) pId FROM xbdun_sys_menu a, xbdun_sys_org_menus b WHERE a.code = b.code AND b.orgCode = ${orgCode} AND a.blDel = 2 ORDER BY a.sort ASC;")
	public List<MenuNodeDto> querySelfOrgMenu(@Param("orgCode")long orgCode,@Param("menuIds")String menuIds);

	//获取到平台拥有的有效权限菜单
	@Select("SELECT CAST(code AS CHAR) menuCode,CONCAT(name,'^',hit) menuNameHit FROM xbdun_sys_menu WHERE code LIKE CONCAT(#{code},'%') AND menuType <>1 AND blDel=2 AND enable = 1")
	public List<PlatformMenuDto> getPlatformMenu(@Param("code")int platCode);

	//获取菜单名称描述
	@Select("SELECT CONCAT(name,'^',hit) FROM xbdun_sys_menu WHERE code=#{menuKey} AND menuType <>1 AND blDel=2 AND enable = 1")
	public String getSysPlatAuth(@Param("menuKey")String menuKey);

	//获取指定平台下的菜单树
	@Select("SELECT m.code id,LEFT(m.code,LENGTH(m.code)-3) pId,m.name,m.hit title,m.menuType type FROM xbdun_sys_menu m WHERE m.code LIKE CONCAT(IF(${code}=0,'',${code}),'%') AND m.blDel = 2 ORDER BY m.sort")
	public List<MenuNodeDto> queryAllMenuTree(@Param("code")long code);

	//获取菜单中属于平台的菜单
	@Select("SELECT id,code,name,hit FROM xbdun_sys_menu WHERE code LIKE '___' AND blDel=2 ORDER BY sort")
	public List<MenuPlatDto> getAllPlatByMenu();

	//获取所有的平台集合
	@Select("SELECT platCode,platName,platDescrip FROM xbdun_platform_info WHERE enabled = 1")
	public List<PlatformInfoDto> getAllPlat();
	
	//获取菜单的使用次数
	@Select("SELECT COUNT(*) FROM (SELECT code FROM xbdun_power_role_depart a WHERE a.code = ${code} UNION SELECT code FROM xbdun_power_user_spec b WHERE b.code = ${code} AND type = 1) c")
	public Long getMenuUsedCount(@Param("code")Long code);
	//删除菜单
	@Delete("DELETE FROM xbdun_sys_org_menus WHERE code=${code}")
	public void deleteOrgMenuByCode(@Param("code")Long code);
	@Delete("DELETE FROM xbdun_sys_menu WHERE code=${code}")
	public void deleteMenuByCode(@Param("code")Long code);

	//获取机构对应的所有权限
	@Select("SELECT GROUP_CONCAT(CAST(code AS CHAR)) FROM xbdun_sys_org_menus WHERE orgCode = #{orgCode} AND code LIKE CONCAT(#{platCode},'%')")
	public String getCurrentOrgAuth(@Param("orgCode")long orgCode,@Param("platCode")int platCode);
	
	@Select("SELECT COUNT(*) FROM xbdun_sys_menu WHERE code LIKE CONCAT(${code},'%')")
	public Long getChildMenu(@Param("code")Long code);
}